package com.whu.waterworks.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.QualityReportDataDao;
import com.whu.waterworks.model.Antisseptic;
import com.whu.waterworks.model.QualityNonNormol;
import com.whu.waterworks.model.QualityNormol;
import com.whu.waterworks.model.WaterPlantNorm;

public class QualityReportDataDaoImpl implements
		QualityReportDataDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}	
	
	
	@Override
	public WaterPlantNorm getWaterPlantNormById(String waterPlantId) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<WaterPlantNorm> rs =(ArrayList<WaterPlantNorm>) hibernateTemplate.find("from  WaterPlantNorm _WaterPlantNorm  where _WaterPlantNorm.waterPlantId='"+waterPlantId+"'");		
		if(null!=rs && rs.size()>0){
			return rs.get(0);
		}
		return null;
	}
	
	@Override
	public ArrayList<QualityNormol> getQualityNormolById(String waterPlantId) {
		// TODO Auto-generated method stub
		return (ArrayList<QualityNormol>) hibernateTemplate.find("from QualityNormol _QualityNormol where _QualityNormol.waterplantId='"+waterPlantId+"'");
	}
	
	

	@Override
	public QualityNormol getLimitedQualityNormol() {
		// TODO Auto-generated method stub
				@SuppressWarnings("unchecked")
				ArrayList<QualityNormol> rs =(ArrayList<QualityNormol>) hibernateTemplate.find("from QualityNormol _QualityNormol where _QualityNormol.type='1'");//where _QualityNormol.type='1'
				if(null!=rs && rs.size()>0){
					return rs.get(0);
				}
				return null;
	}

	@Override
	public String getQualityNormolStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _QualityNormol."+testItem+" from QualityNormol _QualityNormol where _QualityNormol.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}		
	}
	
	@Override
	public String getQualityNonNormolStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _QualityNonNormol."+testItem+" from QualityNonNormol _QualityNonNormol where _QualityNonNormol.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}	
	}
	
	@Override
	public ArrayList<QualityNonNormol> getQualityNonNormolById(
			String waterPlantId) {
		// TODO Auto-generated method stub
		return (ArrayList<QualityNonNormol>) hibernateTemplate.find("from QualityNonNormol _QualityNonNormol where _QualityNonNormol.waterplantId='"+waterPlantId+"'");
	}

	@Override
	public QualityNonNormol getLimitedQualityNonNormol() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<QualityNonNormol> rs =(ArrayList<QualityNonNormol>) hibernateTemplate.find("from QualityNonNormol _QualityNonNormol where _QualityNonNormol.type='1'");
		if(null!=rs && rs.size()>0){
			return rs.get(0);
		}
		return null;
	}

	@Override
	public ArrayList<Antisseptic> getAntissepticById(String waterPlantId) {
		// TODO Auto-generated method stub		
		return (ArrayList<Antisseptic>) hibernateTemplate.find("from Antisseptic _Antisseptic where _Antisseptic.waterplantId='"+waterPlantId+"'");
		
	}

	@Override
	public Antisseptic getLimitedAntisseptic() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<Antisseptic> rs =(ArrayList<Antisseptic>) hibernateTemplate.find("from Antisseptic _Antisseptic where _Antisseptic.type='1'");
		if(null!=rs && rs.size()>0){			
			return rs.get(0);
		}
		return null;
	}

	@Override
	public String getAntissepticStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _Antisseptic."+testItem+" from Antisseptic _Antisseptic where _Antisseptic.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}	
	}

	@Override
	public ArrayList<QualityNormol> getQualityNormolByIdAndMonth(
			String waterPlantId, String time) {
		// TODO Auto-generated method stub
		return (ArrayList<QualityNormol>) hibernateTemplate.find("from QualityNormol _QualityNormol where _QualityNormol.waterplantId='"+waterPlantId+"'and substring(_QualityNormol.time,1,7)='"+time+"' ");
	}

	@Override
	public ArrayList<QualityNonNormol> getQualityNonNormolByIdAndMonth(
			String waterPlantId, String time) {
		// TODO Auto-generated method stub
		return (ArrayList<QualityNonNormol>) hibernateTemplate.find("from QualityNonNormol _QualityNonNormol where _QualityNonNormol.waterplantId='"+waterPlantId+"'and substring(_QualityNonNormol.time,1,7)='"+time+"'");
	}

	@Override
	public ArrayList<Antisseptic> getAntissepticByIdAndMonth(
			String waterPlantId, String time) {
		// TODO Auto-generated method stub
		return (ArrayList<Antisseptic>) hibernateTemplate.find("from Antisseptic _Antisseptic where _Antisseptic.waterplantId='"+waterPlantId+"'and substring(_Antisseptic.time,1,7)='"+time+"'");
	}
	


	
	

}
